using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WhittleList.Helpers;
using WhittleList.Bll;

namespace WhittleList.Controllers
{
    [Authorize]
    public class ProjectController : BaseController
    {

        public ProjectController()
            : this(new UserRepository(), new SiteRepository())
        {
        }

        public ProjectController(IUserRepository userRepository)
            : this(userRepository, new SiteRepository())
        {
        }

        public ProjectController(ISiteRepository siteRepository)
            : this(new UserRepository(), siteRepository)
        {
        }

        public ProjectController(IUserRepository userRepository, ISiteRepository siteRepository)
            : base(userRepository, siteRepository)
        {
        }

        //
        // GET: /Project/
        public ActionResult Index(Site site)
        {

            return View(new ProjectHelpers.IndexHelper() { Site = site, Projects = SiteRepository.Project_GetAllBySiteId(site.Id).ToList() });
        }

        //
        // GET: /Project/Details/5

        public ActionResult Details(Site site, int id)
        {
            var data = SiteRepository.Project_GetById(id);
            if (data != null && data.SiteId == site.Id)
                return View(data);
            else
                throw new NotImplementedException();
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Project/Create

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Site site, string name, string summary)
        {
            var project = new Project();
            project.SiteId = site.Id;
            project.Name = name;
            project.Summary = summary;

            try
            {
                SiteRepository.Insert(project);
                SiteRepository.SaveChanges(base.CurrentUser);
                return RedirectToAction("Details", new { id = project.Id });
            }
            catch (Bll.Exceptions.DuplicateNameException)
            {
                ModelState.AddModelError("Name", "Project Name already exists on this site");
            }
            catch (Bll.Exceptions.ValidationException)
            {
                ModelState.AddModelErrors(project.GetRuleViolations());
            }
            return View(project);
        }

        //
        // GET: /Project/Edit/5
 
        public ActionResult Edit(Site site, int id)
        {
            var data = SiteRepository.Project_GetById(id);
            if (data != null && data.SiteId == site.Id)
                return View(data);
            else
                throw new NotImplementedException();
        }

        //
        // POST: /Project/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Site site, int id, string name, string summary, System.Data.Linq.Binary cid)
        {
            var project = SiteRepository.Project_GetById(id);
            if (project != null && project.SiteId == site.Id && project.CheckConcurrent(cid))
            {
                project.Name = name;
                project.Summary = summary;

                try
                {
                    SiteRepository.SaveChanges(base.CurrentUser);
                    return RedirectToAction("Details", new {id = project.Id });
                }
                catch (Bll.Exceptions.DuplicateNameException)
                {
                    ModelState.AddModelError("Name", "Project Name already exists on this site");
                }
                catch (Bll.Exceptions.ValidationException e)
                {
                    ModelState.AddModelErrors(e.RuleViolations);
                }
                return View(project);
            }
            else
                throw new NotImplementedException();
        }
    }
}
